use dataset_county, clear

collapse (sum) flip*, by(year statea)

keep if year >= 1960

egen totalflipdemrep = sum(flipdemrep), by(year)
egen totalfliprepdem = sum(fliprepdem), by(year)

replace flipdemrep = 0 if flipdemrep == .
replace fliprepdem = 0 if fliprepdem == .

gen key = inlist(statea, "OH", "IA", "WI", "PA", "MI")
keep if key == 1

egen regionflipdemrep = sum(flipdemrep), by(year)
egen regionfliprepdem = sum(fliprepdem), by(year)

*** figure 3a ***

local j = 1
levelsof statea, local(levels)
foreach state of local levels {

	local text1 text(65 1988 "R{&rarr}R{&rarr}D", col(gray))
	local text2 text(42 2000 "D{&rarr}D{&rarr}R", col(black))
	if `j' > 1 local text1
	if `j' > 1 local text2

	#delimit;

	gr tw
		(con flipdemrep year if statea == "`state'", col(black))
		(con fliprepdem year if statea == "`state'", col(gray) msym(s))
		,
			xlab(1960(4)2016, angle(45))
			ylab(, angle(horiz) nogrid)
			legend(off)
			plotregion(style(none))
			xtitle("")
			ytitle("")
			nodraw
			name(g`j', replace)
			title("`state'")
			`text1'
			`text2'
			;
	
	#delimit cr
	
	local mygraphs `mygraphs' g`j'
	
	local j = `j' + 1
	
}

#delimit;

gr tw
	(con regionflipdemrep year if statea == "MI", col(black))
	(con regionfliprepdem year if statea == "MI", col(gray) msym(s))
	,
		xlab(1960(4)2016, angle(45))
		ylab(, angle(horiz) nogrid)
		legend(off)
		plotregion(style(none))
		xtitle("")
		ytitle("")
		nodraw
		name(g`j', replace)
		title("Combined")
		;

#delimit cr

local mygraphs `mygraphs' g`j'

gr combine g1 g2 g3 g4 g5 g6, xcommon l1title("Number of counties") ///
	b1title("Year") imargin(zero) ysize(2) xsize(5.5)
gr export "figure 3a.pdf", replace

*** figure 3b ***

gen totalflip = totalflipdemrep + totalfliprepdem
gen flip = (flipdemrep + fliprepdem)/totalflip

replace flipdemrep = flipdemrep / totalflipdemrep
replace fliprepdem = fliprepdem / totalfliprepdem

replace regionflipdemrep = regionflipdemrep / totalflipdemrep
replace regionfliprepdem = regionfliprepdem / totalfliprepdem

local j = 1
levelsof statea, local(levels)
foreach state of local levels {

	#delimit;

	gr tw
		(con flipdemrep year if statea == "`state'", col(black))
		(con fliprepdem year if statea == "`state'", col(gray) msym(s))
		,
			xlab(1960(4)2016, angle(45))
			ylab(, angle(horiz) nogrid)
			legend(off)
			plotregion(style(none))
			xtitle("")
			ytitle("")
			nodraw
			name(g`j', replace)
			title("`state'")
			;
	
	#delimit cr
	
	local mygraphs `mygraphs' g`j'
	
	local j = `j' + 1
	
}

#delimit;

gr tw
	(con regionflipdemrep year if statea == "MI", col(black))
	(con regionfliprepdem year if statea == "MI", col(gray) msym(s))
	,
		xlab(1960(4)2016, angle(45))
		ylab(, angle(horiz) nogrid)
		legend(off)
		plotregion(style(none))
		xtitle("")
		ytitle("")
		nodraw
		name(g`j', replace)
		title("Combined")
		;

#delimit cr

local mygraphs `mygraphs' g`j'

gr combine g1 g2 g3 g4 g5 g6, xcommon l1title("Proportion of all counties") ///
	b1title("Year") imargin(zero) ysize(2) xsize(5.5)

gr export "figure 3b.pdf", replace

*** figure 3c ***

use dataset_county, clear

gen flipvotes = demvotes + repvotes if flipdemrep == 1 | fliprepdem == 1 

collapse (sum) flipvotes demvotes repvotes, by(year statea)

gen flipshare = flipvotes / (demvotes + repvotes)
keep if year >= 1960

gen key = inlist(statea, "OH", "IA", "WI", "PA", "MI")
keep if key == 1

local j = 1
levelsof statea, local(levels)
foreach state of local levels {

	#delimit;

	gr tw
		(con flipshare year if statea == "`state'", col(black))
		,
			xlab(1960(4)2016, angle(45))
			ylab(, angle(horiz) nogrid)
			legend(off)
			plotregion(style(none))
			xtitle("")
			ytitle("")
			nodraw
			name(g`j', replace)
			title("`state'")
			;
	
	#delimit cr
	
	local mygraphs `mygraphs' g`j'
	
	local j = `j' + 1
	
}

gr combine g1 g2 g3 g4 g5, xcommon l1title("Proportion of all votes") ///
	b1title("Year") imargin(zero) ysize(2) xsize(5.5)

gr export "figure 3c.pdf", replace

		
		
